/*
Designer   : Renyangang

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pins

import (
	"fpga-vboard/bitset"
)

type PinDirection int

const (
	PinDirectionInput  PinDirection = 0
	PinDirectionOutput PinDirection = 1
	PinDirectionInout  PinDirection = 2
)

type PinDef struct {
	Name      string
	BitSize   uint
	Direction PinDirection // 0: input, 1: output 2: inout
}

type PinBitPair struct {
	InputBitStart  uint
	InputBitEnd    uint
	OutputBitStart uint
	OutputBitEnd   uint
}

type PinMap struct {
	InputPinName  string
	OutputPinName string
	InputDevId    int
	OutputDevId   int
	PinBitPair    *PinBitPair
}

func NewPinDef(name string, bitSize uint, direction PinDirection) *PinDef {
	return &PinDef{
		Name:      name,
		BitSize:   bitSize,
		Direction: direction,
	}
}

func NewPinDefs(names []string, bitSizes []uint, direction PinDirection) []*PinDef {
	sDefs := make([]*PinDef, len(names))
	for i, name := range names {
		sDefs[i] = &PinDef{
			Name:      name,
			BitSize:   bitSizes[i],
			Direction: direction,
		}
	}
	return sDefs
}

type SignalPin struct {
	Name    string
	BitSize uint
	byteLen uint
	Value   *bitset.BitSet
}

func NewSignalPin(name string, bitSize uint) *SignalPin {
	return &SignalPin{
		Name:    name,
		BitSize: bitSize,
		byteLen: (bitSize + 7) / 8,
		Value:   bitset.New(bitSize),
	}
}
